package com.oha.laporrayelchurro.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oha.laporrayelchurro.model.Pais;
import com.oha.laporrayelchurro.model.Partido;
import com.oha.laporrayelchurro.service.PartidoService;

public class PartidoServlet extends BaseServlet{

	/**
	 * serialization
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * pais Service
	 */
	private PartidoService pService = new PartidoService();
	
	/**
	 * accion get
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		showData(req, resp);
	}
	
	/**
	 * Accion post
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Partido model = getModel(req);
		if(model!=null){
			pService.saveOrUpdate(model);
		}
		showData(req, resp);
	}
	
	/**
	 * Muestra los datos
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		List<Partido> pList = pService.getAll();
		req.setAttribute("modelList", pList);
		goToView(req, resp, "/pages/partido.jsp");
	}
	
	/**
	 * Obtiene un objeto pais desde el request
	 * @param req
	 * @return
	 */
	private Partido getModel(HttpServletRequest req){
		Partido model = new Partido();
		model.setTipo(getLongParam(req, "tipo"));
		model.setUsuario(getParam(req, "usuario"));
		model.setPais1(getParam(req, "pais1"));
		model.setPais2(getParam(req, "pais2"));
		model.setGoles1(getLongParam(req, "goles1"));
		model.setGoles2(getLongParam(req, "goles2"));
		return model;
	}

}
